
$('.datatable-basic').DataTable({
	 "ajax" : {
		"url" : contextpath + "customer/list",
		"data" : function(d) {
            var query = $("#query").serializeObject();
            query["size"] = d.length;
           	query["page"] = (d.start/d.length);
			return query;
		},
		"dataFilter": function(data){
            var json = jQuery.parseJSON( data );
            json.recordsTotal = json.totalElements;
            json.recordsFiltered = json.totalElements;
            json.data = json.content;
 
            return JSON.stringify( json );
        }
	 },
	 "columns": [
	     {"data":"id"},
		 {
			render : function(data,type, row, meta) {
                return '<a href="'+contextpath+'customer/detail/'+row["id"]+'">'+row["name"]+'</a>';
            }
		 },
		 {
				render : function(data,type, row, meta) {
	                return row['level'] + '级';
	            }
			 },
	     {"data" : "recommendDistributorName"},
         { "data": "phoneNo" },
         {
            render : function(data,type, row, meta) {
                return $("#btnGroup").html();
            }
        }
     ],
     "drawCallback": function( settings ) {
		 $(".btn-edit").click(function(){
			/*$('#phoneNo').attr("readonly","readonly")//将input元素设置为readonly
			$("#edit").validate().resetForm();
			var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
		    $("#edit").fill(row);
		    
		    $(".select").trigger("change");
		    $(":input[name='id']").val(row.id);*/
			var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			window.location.href = contextpath + "customer/customerEdit?id=" + row.id;
		 });
		 
		 $(".btn-integration").click(function(){
			 var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			 $(":input[name='integration']").val('');
			 $(":input[name='customerId']").val(row.id);
		 });
		 
		 $(".btn-integration-amount").click(function(){
			 var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			 $(":input[name='amount']").val('');
			 $(":input[name='customerId']").val(row.id);
		 });
		 
		 $(".btn-detail").click(function () {
             var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
             window.location.href = contextpath + "customer/detail/" + row.id;
         });
		 
		 $(".btn-sms").click(function () {
			 var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			 $(":input[name='customerId']").val(row.id);
         });
		 
		 $(".btn-upgrade").click(function(){
			 var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
				swal({
					title : "升级为渠道商",
					text : "确定要升级客户： "+row.name+"成为渠道商?",
					type : "warning",
					showCancelButton : true,
					confirmButtonColor : "#DD6B55",
					confirmButtonText : "确定",
					cancelButtonText: "取消",
					closeOnConfirm : false
				},function() {
					$.post(contextpath + "customer/audit", "id=" + row.id, function(){
						$('.datatable-basic').DataTable().draw();
						$('.close').click();
						swal("升级渠道商成功","", "success");
					});
				});
				
			 });
		 
		 $(".btn-del").click(function(){
			var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			swal({
				title : "修改状态",
				text : "确定要删除客户： "+row.name+"?",
				type : "warning",
				showCancelButton : true,
				confirmButtonColor : "#DD6B55",
				confirmButtonText : "确定",
				cancelButtonText: "取消",
				closeOnConfirm : false
			},function() {
				$.get(contextpath + "customer/delete/" + row.id, function(d){
					$('.datatable-basic').DataTable().draw();
					swal("删除客户成功","", "success");
				})
			});
			
		 });
      }
});

function selectedSendNow(obj){
	var selectedObj = obj.options[obj.options.selectedIndex];
	if(selectedObj){
		var value = selectedObj.value;
	    var text = selectedObj.text;
	    
		if(value == '0'){
			$("#sendTimeDiv").css("display", "none");
			$("#hourDiv").css("display", "none");
		}else{
			$("#sendTimeDiv").css("display", "block");
			$("#hourDiv").css("display", "block");
		}
	}
};

$(function(){
	/*$(".btn-add").click(function(){
		$("#edit").validate().resetForm();
		
		$('#phoneNo').removeAttr("readonly");//去除input元素的readonly属性
         $(":input[name='type']").removeAttr("disabled");
         $(":input[name='auditStatus']").removeAttr("disabled");
         $(":input[name='status']").removeAttr("disabled");
		$("#edit")[0].reset();
		$(":input[name='id']").val();
		$("#cityId").select2("val", ""); 
		$("#areaId").select2("val", ""); 
		$("#cityId").empty();
		$("#areaId").empty();
	});*/
	
	$(".btn-add").click(function(){
		window.location.href = contextpath + "customer/customerEdit?currentPage=" + $(".current").text();
	});
	
	$("#submitIntegration").click(function(){
		if($("#integrationForm").valid()){
			$.post(contextpath + "customer/adjustIntegration", $("#integrationForm").serialize(), function(){
				$('.datatable-basic').DataTable().draw();
				$('.close').click();
				swal("积分操作成功","", "success");
			})
		}
	});
	
	$("#submitIntegrationAmount").click(function(){
		if($("#integrationAmountForm").valid()){
			$.post(contextpath + "customer/adjustIntegration", $("#integrationAmountForm").serialize(), function(){
				$('.datatable-basic').DataTable().draw();
				$('.close').click();
				swal("编辑客户积分成功","", "success");
			})
		}
	});
	
	$("#submitEdit").click(function(){
		if($("#edit").valid()){
			
			var param = $("#edit").serialize() + "";
			param = param.replace(/=-1/g, "=");
			
			$.post(contextpath + "customer/save", param, function(){
				/*$(":input[name='type']").removeAttr("disabled");
		         $(":input[name='auditStatus']").removeAttr("disabled");
		         $(":input[name='status']").removeAttr("disabled");*/
				$('.datatable-basic').DataTable().draw();
				$('.close').click();
				swal("编辑客户成功","", "success");
			})
		}
	});
	 
	$("#idCardNo").blur(function(){
		var idcard = $("#idCardNo").val();//获得身份证号
		var year = idcard.substr(6,4);
		var month = idcard.substr(10,2);
		var day = idcard.substr(12,2);
		var date = year+"-"+month+"-"+day;
		if(year && month &&  day){
			$("#birthday").val(date);
		}
	});
	
	$("#submit").click(function(){
		$('.datatable-basic').DataTable().draw();
	});

	$('select').select2({
	    minimumResultsForSearch: Infinity
	});	

	$('#birthday').daterangepicker(
			  { 
				  "singleDatePicker": true,
				  "parentEl":$("#edit_dialog"),
				  showDropdowns: true,
				  startDate: moment().subtract('years', 30),
			    locale:{
			    	format: 'YYYY-MM-DD',
		            applyLabel: '确认',
		            cancelLabel: '取消',
		            fromLabel: '从',
		            toLabel: '到',
		            customRangeLabel: '选择时间',
		            daysOfWeek:["日","一","二","三","四","五","六"],
		            monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
		        }
			  }
			);
	 $('#birthday').val("");
	
	 
	 $("#submitSms").click(function(){
			if($("#saveSms").valid()){
				$.post(contextpath + "customer/saveSms", $("#saveSms").serialize(), function(){
					swal("保存短信信息成功","", "success");
					$('.close').click();
				})
			}
		});

		 $('#sendTime').daterangepicker(
				  { 
					  "singleDatePicker": true,
					  "parentEl":$("#sms_dialog"),
				    locale:{
				    	format: 'YYYY-MM-DD',
			            applyLabel: '确认',
			            cancelLabel: '取消',
			            fromLabel: '从',
			            toLabel: '到',
			            customRangeLabel: '选择时间',
			            daysOfWeek:["日","一","二","三","四","五","六"],
			            monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
			        }
				  }
				);
		 
})
